<template>
  <view class="cart">
    <!-- 1 列表 -->
    <view class="cart-list">
      <view class="cart-item" v-for="goods in cartList" :key="goods.goods_id">
        <!-- 1 复选框 -->
        <view class="goods-chk">
          <u-checkbox
            :value="goods.checked"
            shape="circle"
            active-color="red"
            @change="handleChangChecked(goods.goods_id)"
          ></u-checkbox>
        </view>
        <!-- 2 图片 -->
        <view class="goods-img">
          <u-image
            width="191rpx"
            height="191rpx"
            :src="goods.goods_small_logo"
          ></u-image>
        </view>
        <!-- 3 信息 -->
        <view class="goods-info">
          <!-- 名称 -->
          <view class="goods-name u-line-2">{{ goods.goods_name }}</view>
          <!-- 价格和数量 -->
          <view class="goods-price-num">
            <view class="goods-price">￥{{ goods.goods_price }}</view>
            <view class="goods-num-tool">
              <view
                class="num-btn"
                @click="changCartNum(-1, goods.goods_id, goods.num)"
                >-</view
              >
              <view class="goods-num">{{ goods.num }}</view>
              <view
                class="num-btn"
                @click="changCartNum(+1, goods.goods_id, goods.num)"
                >+</view
              >
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-chk">
        <u-checkbox :value="isAllChecked" shape="circle" active-color="red" @change="allChecked"
          >全选</u-checkbox
        >
      </view>
      <view class="all-price"> 合计: <text>￥{{priceTotal}}</text> </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="handleToPay">去结算({{cartTotal}})</u-button>
      </view>
    </view>
  </view>
</template>

<script>
import { mapState, mapMutations, mapGetters } from "vuex";
export default {
  computed: {
    ...mapState("cart", ["cartList"]),
    ...mapGetters("cart", ["isAllChecked","cartTotal","priceTotal"]),
  },
  methods: {
    ...mapMutations("cart", ["changChecked", "cartNumUpdate","delCart","allCheckedToggle"]),
    handleChangChecked(goods_id) {
      this.changChecked(goods_id);
    },
    async changCartNum(unit, goods_id, num) {
      if (unit === -1 && num === 1) {
        const [err, res] = await uni.showModal({
          title: "提示",
          content: "确定删除该宝贝？",
        });
        if (err) {
          console.log(err);
          return;
        }
       
        if (res.confirm) {
         this.delCart({goods_id})
        }
      }else{
        this.cartNumUpdate({ unit, goods_id });
      }

    },
    allChecked(){
      this.allCheckedToggle({value:!this.isAllChecked})
    },
    handleToPay(){
      if(this.cartTotal === 0){
       uni.showToast({
          title: '请选择宝贝',
          icon: 'none',
        });
          return
      }
      uni.navigateTo({
        url: '/pages/pay/pay'
      });
        
    }
  },
};
</script>

<style lang="scss">
.cart {
  padding-bottom: 111rpx;
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: $theme-color;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: space-between;
            .num-btn {
              border: 1rpx solid #ccc;
              border-radius: 50%;
              width: 40rpx;
              height: 40rpx;
              text-align: center;
              line-height: 40rpx;
            }
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;
    .all-chk {
    }

    .all-price {
      flex: 1;
      color: #666;
      text {
      }
    }

    .all-count {
    }
  }
}
</style>